#include "Outils.h"
#include <stdlib.h>
#include <string.h>
/* This is a callback function. The data arguments are ignored
 * in this example. More on callbacks below.
 
 


*INSTRUCTIONS DE COMPILATION

*SUPPOSE QUE VOUS AYEZ LA LIBRAIRIE GTK+ 3.0 DANS LE CHEMIN INCLUDE DE BASE DU COMPILATEUR

*You can compile the program above with GCC using: 
*

*
*

** gcc `pkg-config --cflags gtk+-3.0 gmodule-2.0` -o XWinMain XWinMain.c `pkg-config --libs gtk+-3.0 gmodule-2.0`

 
 */

// stockage global du pointeur vers l'objet saisie texte
GtkEntryBuffer *EntrybufferInt;

// stockaage global d'un entier
int glint;

// le tableau à 100 cellules
int tb_int[100];

// le pointeur vers la section de mémoir qui va contenir le tableau à taille
// variable
void *ptr_tablo;

// l'index courant du tableau
int cudex;

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World in the text entry was ;\n");
  if (EntrybufferInt == NULL) {
  g_printf (" Entry buffer was NULL so avoided extraction\n");
  
  }
  else {
    g_printf (gtk_entry_buffer_get_text (EntrybufferInt));
  g_printf ("END\n");
  }
}

int isstrictlynumeric(char *strin) {

//    return strcmp(itoa(atoi(strin)), strin) == 0;
 
  if (!strin) {
  	return 1;
  }
  else {
  	return 0;
  }
  
}

static gboolean
on_delete_event (GtkWidget *widget,
                 GdkEvent  *event,
                 gpointer   data)
{
  /* If you return FALSE in the "delete_event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window to be destroyed.
   *
   * This is useful for popping up 'are you sure you want to quit?'
   * type dialogs.
   */

  g_print ("delete event occurred\n");
 g_printf ("Current int : %d",glint++);
  g_printf ("\n");

  return TRUE;
}

G_MODULE_EXPORT gboolean on_quittApp_event (GtkWidget *widget,
                 GdkEvent  *event,
                 gpointer   data)
{
  /* If you return FALSE in the "delete_event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window to be destroyed.
   *
   * This is useful for popping up 'are you sure you want to quit?'
   * type dialogs.
   */

  g_print ("quittApp Invoked \n");
 g_printf ("Current int : %d \n",glint++);
  g_printf ("\n");

  return TRUE;
}

G_MODULE_EXPORT gboolean on_InsInt_event (GtkWidget *widget,
                 GdkEvent  *event,
                 gpointer   data)
{
  /* If you return FALSE in the "delete_event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window to be destroyed.
   *
   * This is useful for popping up 'are you sure you want to quit?'
   * type dialogs.
   */

  g_print ("InsInt Invoked \n");

if (isstrictlynumeric( (char *)gtk_entry_buffer_get_text (EntrybufferInt))) {
	glint = atoi(gtk_entry_buffer_get_text (EntrybufferInt));
  g_printf ("foud it to be INT %d\n",glint);
}
else {
  g_printf ("fwas not Int!!!\n");

}

  return TRUE;
}

//on_entryDeclare_event
G_MODULE_EXPORT gboolean on_entryDeclare_event (GtkEntry *widget,
                 GdkEvent  *event,
                 gpointer   data)
{
  /* If you return FALSE in the "delete_event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window to be destroyed.
   *
   * This is useful for popping up 'are you sure you want to quit?'
   * type dialogs.
   */

  g_print ("quittApp Invoked \n");
  if (EntrybufferInt == NULL) {
  g_printf (" Had to assign the buffer (was NULL)\n");
EntrybufferInt = gtk_entry_get_buffer (widget);
}

  return TRUE;
}

G_MODULE_EXPORT gboolean on_textUpdate_event (GtkEntry *widget,
                 GdkEvent  *event,
                 gpointer   data)
{
  /* If you return FALSE in the "delete_event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window to be destroyed.
   *
   * This is useful for popping up 'are you sure you want to quit?'
   * type dialogs.
   */
//GtkEntryBuffer *buffer;

//gchar *centy = "                                  ";

gchar *eventss = "";
   if (event == NULL) {
   eventss = "Crudd";
   }
   else {
   eventss = (gchar *) event;
   }
  g_printf ("Entry creation  Invoked $eventss \n");
  if (EntrybufferInt == NULL) {
  g_printf (" Had to assign the buffer (was NULL)\n");
EntrybufferInt = gtk_entry_get_buffer (widget);
}
  g_printf (gtk_entry_buffer_get_text (EntrybufferInt));
  g_printf ("\n");

//centy = gtk_entry_buffer_get_text (EntrybufferInt);


  return TRUE;
}

G_MODULE_EXPORT gboolean on_ViderTabl_event (GtkEntry *widget,
                 GdkEvent  *event,
                 gpointer   data)
{
  /* If you return FALSE in the "delete_event" signal handler,
   * GTK will emit the "destroy" signal. Returning TRUE means
   * you don't want the window tabl[curdex]to be destroyed.
   *
   * This is useful for popping up 'are you sure you want to quit?'
   * type dialogs.
   */

gchar *eventss = "";
   if (event == NULL) {
   eventss = "Crudd";
   }
   else {
   eventss = (gchar *) event;
   }
  g_printf (" EMPTY TABLE Invoked $eventss \n");
  if (EntrybufferInt == NULL) {
  g_printf (" Entry buffer was NULL so avoided extraction\n");
  
  }
  else {
  g_printf (gtk_entry_buffer_get_text (EntrybufferInt));
  g_printf ("\n");
}
  return TRUE;
}

void mp_g_printf(gchar *stt,...) {
   g_printf(stt);
}

int creer_pile(int taille, int index) {

// ptr_tablo est un pointeur déclaré en top niveau, donc visible partout
ptr_tablo = (int *)malloc(taille * sizeof(int));

// cudex est un entier au niveau global
cudex = index;

if (ptr_tablo == NULL) {

return 0;
}
else
{
return 1;
}

}

int essai_table() {
 int yuu;
  int *ree = NULL;
   int tabl[100];
  int curdex;
  curdex = 0;
  char uui[4] = "juu";
  
  yuu = 45;
  g_printf(" yuu is %d\n",yuu);
  ree = malloc(sizeof(int));
 *ree = 65;
  g_printf(" ree is %d\n",*ree);
  memcpy(ree,&yuu,sizeof(int));
  g_printf(" ree is now %d\n",*ree);
  
 int i;
   for (i = 0; i <= 99; i++) {
  	tabl[i] = 0;
  }
  tabl[curdex] = 200;
  
  if (creer_pile(200,199)) {
  
  memcpy(ptr_tablo,ree,sizeof(int));
  
  int i;
   for (i = 0; i <= 99; i++) {
  *ree = *ree + 10; 
  memcpy(ptr_tablo + (i),ree,sizeof(int));
  //ree = (int *)ptr_tablo;
  g_printf(" ptr-tablo at %d  is now %d\n",i,*(int *)(ptr_tablo + i));
 }  
 
  *ree = 65555;
 
   memcpy(ptr_tablo + (cudex),ree,sizeof(int));
   g_printf(" ptr-tablo  at %d is now %d\n",cudex,*(int *)(ptr_tablo + cudex));
 
  }
mp_g_printf (" Contenu de table copied yuu %d\n",yuu);

mp_g_printf (" Contenu de table ree %d\n",ree);

mp_g_printf (" Contenu de table ree adv %d \n",ree);
  
  glint = tabl[curdex];

mp_g_printf (" Contenu de table test %d\n",tabl[curdex]);
  g_printf ("\n");


return 1;

} // essai_table


int
main (int   argc,
      char *argv[])
{
  /* GtkWidget is the storage type for widgets */
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *button2;
  GtkBuilder *builder;
 GtkBuilder *message_b;
   GtkWidget *dialo;
   
  if (essai_table()) {
  	// essai table s'est bien passé
  }
 
  /* This is called in all GTK applications. Arguments are parsed
   * from the command line and are returned to the application.
   */
  gtk_init (&argc, &argv);

  /* Construct a GtkBuilder instance and load our UI description */
  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "builder.ui", NULL);


  message_b = gtk_builder_new ();
  gtk_builder_add_from_file (message_b, "message.ui", NULL);
  
    /* Connect signals */
    gtk_builder_connect_signals( builder, NULL );
    gtk_builder_connect_signals( message_b, NULL );
  /* create a new window, and set its title */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "NSY103 - ExoNavi");
  
  
  dialo = (GtkWidget *)gtk_builder_get_object(message_b,"MessageR");
  
  

  /* When the window emits the "delete-event" signal (which is emitted
   * by GTK+ in response to an event coming from the window manager,
   * usually as a result of clicking the "close" window control), we
   * ask it to call the on_delete_event() function as defined above.
   *
   * The data passed to the callback function is NULL and is ignored
   * in the callback function.
   */
  g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);

  /* Here we connect the "destroy" event to the gtk_main_quit() function.
   *
   * This signal is emitted when we call gtk_widget_destroy() on the window,
   * or if we return FALSE in the "delete_event" callback.
   */
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  /* Sets the border width of the window. */
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);

  /* Creates a new button with the label "Hello World". */
  button = gtk_button_new_with_label ("Appli 1");
  button2 = gtk_button_new_with_label ("Appli 2");

  /* When the button receives the "clicked" signal, it will call the
   * function print_hello() passing it NULL as its argument.
   *
   * The print_hello() function is defined above.
   */
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  //g_signal_connect (button2, "clicked", G_CALLBACK (print_hello), NULL);

  /* The g_signal_connect_swapped() function will connect the "clicked" signal
   * of the button to the gtk_widget_destroy() function; instead of calling it
   * using the buttabl[curdex]ton as its argument, it will swap it with the user data
   * argument. This will cause the window to be destroyed by calling
   * gtk_widget_destroy() on the window.
   */
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
 g_signal_connect_swapped (button2, "clicked", G_CALLBACK (gtk_widget_destroy), window);

  /* This packs the button into the window. A GtkWindow inherits from GtkBin,
   * which is a special container that can only have one child
   */
  gtk_container_add (GTK_CONTAINER (window), button);
  //gtk_container_add (GTK_CONTAINER (window), button2);

  /* The final step is to display this newly created widget... */
  gtk_widget_show (button);
  //gtk_widget_show (button2);

  /* ... and the window */
  gtk_widget_show (window);


  gtk_widget_show (dialo);

  /* All GTK applications must have a gtk_main(). Control ends here
   * and waits for an event to occur (like a key press or a mouse event),
   * until gtk_main_quit() is called.
   */
  gtk_main ();

  return 0;
}
 


